﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RestaurantCommon;

namespace Restaurant.Controls
{
    public partial class TablesUserControl : UserControl, ITableList
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.TableSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public TablesUserControl()
        {
            InitializeComponent();
            gridView.DoubleClick += gridView_DoubleClick;
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            MesaDataSet dataSet = _controller.Tables();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Mesa.TableName;
        }

        private void ShowTable(long tableId)
        {
            var window = new TableXtraForm { TableId = tableId, Controller = _controller };
            window.ShowDialog();           
        }

        #endregion

        #region ITableList

        public void AddTable()
        {
            ShowTable(-1);
        }
        
        #endregion

        void gridView_DoubleClick(object sender, EventArgs e)
        {
            var table = (MesaDataSet.MesaRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (table == null)
                return;
            ShowTable(table.Id);
        }
    }
}
